/**
 * 
 */
package com.quiz.activity.questions;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;

import com.quiz.dao.PlayerDatasource;
import com.quiz.dao.SectionDataSource;
import com.quiz.model.Player;
import com.quiz.model.Section;


/**
 *
 * @author Brian Boyle
 * Apr 15, 2012
 */
public class QuestionsFinishedClickListener implements OnClickListener {

    private Context context;
    private Section section;
    private int correctQuestionCount;
    private int score;
    private SectionDataSource sectionDatasource;
    private PlayerDatasource playerDatasource;

    public QuestionsFinishedClickListener(Context context, Section section, int correctQuestionCount, int score) {
        this.context = context;
        this.section = section;
        this.correctQuestionCount = correctQuestionCount;
        this.score = score;
    }

    /**
     * {@inheritDoc}
     */
    public void onClick(View v) {

        sectionDatasource = new SectionDataSource(this.context);
        playerDatasource = new PlayerDatasource(this.context);
        Player player = playerDatasource.getPlayerDetails();
        boolean playerHasNewRank = false;

        if (score > section.getTopScore()) {

            int newPlayerHighScore = player.getHighScore() + score;
            //Subtract the original topScore for the section and then replace it with the new topScore for the section
            if (section.getTopScore() > 0) {
                newPlayerHighScore = newPlayerHighScore - section.getTopScore();
            }
            //Update section score
            section.setTopScore(score);
            sectionDatasource.updateSectionTopScore(section);
            //TODO ADD LIVES BONUS
            //Add the score to the player's current highscore to increase their total
            System.out.println("Setting highscore to: " + newPlayerHighScore);
            player.setHighScore(newPlayerHighScore);
        }

        //Update the player's ranking if they have reached the score needed for the next section
        Section nextSection = null;
        if (section.getId() != 6) {
            nextSection = sectionDatasource.getSectionById(section.getId() + 1);
            if (nextSection.getStatus().equals("LOCKED")) {
                if (player.getHighScore() >= nextSection.getScoreNeeded()) {
                    player.setCurrentRank(nextSection.getName());
                    sectionDatasource.unlockSection(nextSection.getId());
                    playerHasNewRank = true;
                }
            }
        }
        playerDatasource.updatePlayer(player);

        sectionDatasource.close();
        playerDatasource.close();
        Intent roundSummaryIntent = new Intent(this.context, RoundSummaryActivity.class);
        roundSummaryIntent.putExtra("correctQuestionCount", this.correctQuestionCount);
        roundSummaryIntent.putExtra("playerHasNewRank", playerHasNewRank);
        roundSummaryIntent.putExtra("currentSectionId", section.getId());
        if (playerHasNewRank) {
            roundSummaryIntent.putExtra("nextSectionId", nextSection.getId());
        }
        roundSummaryIntent.putExtra("pointsScored", score);
        roundSummaryIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        this.context.startActivity(roundSummaryIntent);
    }
}
